package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Carousel;
import com.sfzy.service.CarouselService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("carousel")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;
    //增
    @PostMapping
    public R addCarousel(@RequestBody Carousel carousel) {
        int i = carouselService.addCarousel(carousel);
        if(i==0){
            return R.fail("增加失败！");
        }
        return R.ok("增加成功！");
    }
    //删
    @DeleteMapping("{id}")
    public R deleteCarousel(@PathVariable("id") int id) {
        int i = carouselService.deleteCarousel(id);
        if(i==0){
            return R.fail("删除失败！");
        }
        return R.ok("删除成功！");
    }
    //改
    @PutMapping
    public R updateCarousel(@RequestBody Carousel carousel) {
        int i = carouselService.updateCarousel(carousel);
        if(i==0){
            return R.fail("修改失败！");
        }
        return R.ok("修改成功！");
    }
    //根据id查
    @GetMapping("{id}")
    public R getCarouselById(@PathVariable("id") int id) {
        Carousel carouselById = carouselService.getCarouselById(id);
        return R.ok(carouselById);
    }
    //分页查
    @GetMapping("page")
    public R getAllCarousel(Carousel carousel,
                            @RequestParam(required = false,defaultValue = "1") int pageNum,
                            @RequestParam(required = false,defaultValue = "5")int pageSize) {
        PageInfo<Carousel> allCarousel = carouselService.getAllCarousel(carousel, pageNum, pageSize);
        return R.ok(allCarousel);
    }
}
